package com.jackrain.nea.fc.vp.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.vp.api.ViewCvipAddrQueryCmd;
import com.jackrain.nea.fc.vp.mapper.ViewCvipAddrMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * FC-会员接口-VIP收货地址-FC新增/修改 —> F4
 *
 * @author WSL
 * @create 2017/11/16
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "vp")
public class ViewCvipAddrQueryCmdImpl extends CommandAdapter implements ViewCvipAddrQueryCmd {

    @Reference(version = "2" , group = "cp")
    private QueryCmd queryCmd;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        // 常量
        String cpcProId = "CP_C_PRO_ID";
        String cpcCityId = "CP_C_CITY_ID";
        String cpcDistId = "CP_C_DIST_ID";
        String y = "Y";
        String isDefault = "ISDEFAULT";

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCvipAddrQueryCmdImpl--param-->{}",param);

        if ((null == param) || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        // Mapper
        ViewCvipAddrMapper viewCvipAddrMapper = ApplicationContextHandle.getBean(ViewCvipAddrMapper.class);

        // 根据param查询条件 查询【VIP收货地址】表
        List<HashMap> list = viewCvipAddrMapper.selectProvider(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {
            // VIP卡号
            String vipId = viewCvipAddrMapper.selVipAccEcode(TypeUtils.castToLong(hashMap.get("VP_C_VIP_ID")));
            hashMap.put("VP_C_VIP_ID",vipId);

            // VIP类型
            hashMap.put("VP_C_VIPTYPEGROUP_ID","FF");

            // 省份
            if (null != hashMap.get(cpcProId)){

                HashMap hashMapOther = new HashMap(16);
                hashMapOther.put("tableName", "CP_C_REGION");
                hashMapOther.put("fields", "ENAME");
                JSONObject whereJson = new JSONObject();
                whereJson.put("REGIONTYPE", "PROV");
                whereJson.put("ID",TypeUtils.castToLong(hashMap.get("CP_C_PRO_ID")));
                hashMapOther.put("whereKeys", whereJson);
                hashMapOther.put("type",HashMap.class);
                HashMap dataMap = (HashMap) queryCmd.queryTable(hashMapOther);
                if (null == dataMap || dataMap.size() == 0){
                    throw new NDSException("根据省ID未查询到数据！");
                }
                Object pro = dataMap.get("ENAME");

                hashMap.put("CP_C_PRO_ID",pro);
            }else{
                hashMap.put("CP_C_PRO_ID",null);
            }

            // 城市
            if (null != hashMap.get(cpcCityId)){

                HashMap hashMapOther = new HashMap(16);
                hashMapOther.put("tableName", "CP_C_REGION");
                hashMapOther.put("fields", "ENAME");
                JSONObject whereJson = new JSONObject();
                whereJson.put("REGIONTYPE", "CITY");
                whereJson.put("ID",TypeUtils.castToLong(hashMap.get("CP_C_CITY_ID")));
                hashMapOther.put("whereKeys", whereJson);
                hashMapOther.put("type",HashMap.class);
                HashMap dataMap = (HashMap) queryCmd.queryTable(hashMapOther);
                if (null == dataMap || dataMap.size() == 0){
                    throw new NDSException("根据市ID未查询到数据！");
                }
                Object city = dataMap.get("ENAME");

                hashMap.put("CP_C_CITY_ID",city);
            }else{
                hashMap.put("CP_C_CITY_ID",null);
            }

            // 区县
            if (null != hashMap.get(cpcDistId)){

                HashMap hashMapOther = new HashMap(16);
                hashMapOther.put("tableName", "CP_C_REGION");
                hashMapOther.put("fields", "ENAME");
                JSONObject whereJson = new JSONObject();
                whereJson.put("REGIONTYPE", "DIST");
                whereJson.put("ID",TypeUtils.castToLong(hashMap.get("CP_C_DIST_ID")));
                hashMapOther.put("whereKeys", whereJson);
                hashMapOther.put("type",HashMap.class);
                HashMap dataMap = (HashMap) queryCmd.queryTable(hashMapOther);
                if (null == dataMap || dataMap.size() == 0){
                    throw new NDSException("根据区ID未查询到数据！");
                }
                Object county = dataMap.get("ENAME");

                hashMap.put("CP_C_DIST_ID",county);
            }else{
                hashMap.put("CP_C_DIST_ID",null);
            }

            // 是否默认
            if (y.equals(hashMap.get(isDefault))){
                hashMap.put("ISDEFAULT",1);
            }else{
                hashMap.put("ISDEFAULT",0);
            }

            // 创建人
            HashMap hashMapOther = new HashMap(16);
            hashMapOther.put("tableName", "USERS");
            hashMapOther.put("fields", "ENAME");
            JSONObject whereJson = new JSONObject();
            whereJson.put("ID",TypeUtils.castToLong(hashMap.get("OWNERID")));
            hashMapOther.put("whereKeys", whereJson);
            hashMapOther.put("type",HashMap.class);
            HashMap dataMap = (HashMap) queryCmd.queryTable(hashMapOther);
            if (null == dataMap || dataMap.size() == 0){
                throw new NDSException("根据区ID未查询到数据！");
            }
            Object ownerId = dataMap.get("ENAME");

            hashMap.put("OWNERID",ownerId);

            // 修改人
            HashMap hashMapOthers = new HashMap(16);
            hashMapOthers.put("tableName", "USERS");
            hashMapOthers.put("fields", "ENAME");
            JSONObject whereJsons = new JSONObject();
            whereJsons.put("ID",TypeUtils.castToLong(hashMap.get("MODIFIERID")));
            hashMapOthers.put("whereKeys", whereJsons);
            hashMapOthers.put("type",HashMap.class);
            HashMap dataMaps = (HashMap) queryCmd.queryTable(hashMapOther);
            if (null == dataMaps || dataMaps.size() == 0){
                throw new NDSException("根据区ID未查询到数据！");
            }
            Object modifierId = dataMaps.get("ENAME");
            hashMap.put("MODIFIERID",modifierId);

            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);
        }
        vh.put("data",resultJson);
        log.debug("ViewCvipAddrQueryCmdImpl--resultJson-->{}",resultJson);
//        log.debug("vh ===>"+vh.toJSONObject().toJSONString());
        return vh;
    }
}